package com.muy.redis.common.tree.actions;

import com.intellij.icons.AllIcons;
import com.muy.redis.common.dialog.MrRedisNewKeyDialog;
import com.muy.redis.common.tree.MTTreeCell;
import com.muy.redis.common.tree.TreePanelMark;
import com.muy.redis.view.component.MrRedisEditorJpanelLeft;

import javax.swing.tree.DefaultMutableTreeNode;

/**
 * @Author jiyanghuang
 * @Date 2022/8/4 21:10
 */
public class MrRedisAddKeyAction extends AbstractMTTreeCellSelectTreeAction {

    private TreePanelMark treePanelMark;

    public MrRedisAddKeyAction(TreePanelMark treePanelMark) {
        super("Add", "Add", AllIcons.General.Add, treePanelMark);
        this.treePanelMark = treePanelMark;
    }

    @Override
    public void handleSelectTree(DefaultMutableTreeNode mutableTreeNode, MTTreeCell mtTreeCell) {
        MrRedisEditorJpanelLeft mrRedisEditorJpanelLeft = (MrRedisEditorJpanelLeft)treePanelMark;
//        RedisPoolManager redisPoolManager = MrRedisConnectionUtils.findRedisPoolManager(mrRedisEditorJpanelLeft.getMrRedisConnectionParam());
//        MrRedisDb dbInfo = mrRedisEditorJpanelLeft.getMrRedisDb();
//        NewKeyDialog newKeyDialog = new NewKeyDialog(mrRedisEditorJpanelLeft.getProject());
//        newKeyDialog.setCustomOkAction(actionEvent -> {
//            try {
//                String key = newKeyDialog.getKeyTextField().getText();
//                if (StringUtils.isEmpty(key)) {
//                    ErrorDialog.show("Key can not be empty");
//                    return;
//                }
//
//                String valueString;
//                // 判断数据类型, 并存入
//                switch (newKeyDialog.getSelectedType()) {
//                    case String:
//                        valueString = newKeyDialog.getStringValueTextArea().getText();
//                        if (StringUtils.isEmpty(valueString)) {
//                            ErrorDialog.show("Value can not be empty");
//                        } else {
//                            redisPoolManager.set(key, valueString, 0, dbInfo.getIndex());
//                            // 关闭对话框
//                            newKeyDialog.close(OK_EXIT_CODE);
//                            if (newKeyDialog.isReloadSelected()) {
//                                // 重新渲染keyTree
//                                mrRedisEditorJpanelLeft.resetPageIndex();
//                                mrRedisEditorJpanelLeft.renderKeyTree("*", null);
//                            }
//                        }
//                        break;
//
//                    case List:
//                        valueString = newKeyDialog.getListValueTextArea().getText();
//                        if (StringUtils.isEmpty(valueString)) {
//                            ErrorDialog.show("Value can not be empty");
//                        } else {
//                            try {
//                                List<String> strings = JSON.parseArray(valueString, String.class);
//                                redisPoolManager.lpush(key, strings.toArray(new String[]{}), dbInfo.getIndex());
//                            } catch (Exception exception) {
//                                redisPoolManager.lpush(key, new String[]{valueString}, dbInfo.getIndex());
//                            }
//                            // 关闭对话框
//                            newKeyDialog.close(OK_EXIT_CODE);
//                            if (newKeyDialog.isReloadSelected()) {
//                                // 重新渲染keyTree
//                                mrRedisEditorJpanelLeft.resetPageIndex();
//                                mrRedisEditorJpanelLeft.renderKeyTree("*", null);
//                            }
//                        }
//                        break;
//
//                    case Set:
//                        valueString = newKeyDialog.getSetValueTextArea().getText();
//                        if (StringUtils.isEmpty(valueString)) {
//                            ErrorDialog.show("Value can not be empty");
//                        } else {
//                            try {
//                                List<String> strings = JSON.parseArray(valueString, String.class);
//                                redisPoolManager.sadd(key, dbInfo.getIndex(), strings.toArray(new String[]{}));
//                            } catch (Exception exception) {
//                                redisPoolManager.sadd(key, dbInfo.getIndex(), valueString);
//                            }
//                            // 关闭对话框
//                            newKeyDialog.close(OK_EXIT_CODE);
//                            if (newKeyDialog.isReloadSelected()) {
//                                // 重新渲染keyTree
//                                mrRedisEditorJpanelLeft.resetPageIndex();
//                                mrRedisEditorJpanelLeft.renderKeyTree("*", null);
//                            }
//                        }
//                        break;
//
//                    case Zset:
//                        valueString = newKeyDialog.getZsetValueTextArea().getText();
//                        String score = newKeyDialog.getScoreTextField().getText();
//                        if (StringUtils.isEmpty(valueString)) {
//                            ErrorDialog.show("Value can not be empty");
//                        } else if (StringUtils.isEmpty(score)) {
//                            ErrorDialog.show("Score can not be empty");
//                        } else {
//                            try (Jedis jedis = redisPoolManager.getJedis(dbInfo.getIndex())) {
//                                if (jedis != null) {
//                                    jedis.zadd(key, Double.parseDouble(score), valueString);
//                                }
//                            }
//                            // 关闭对话框
//                            newKeyDialog.close(OK_EXIT_CODE);
//                            if (newKeyDialog.isReloadSelected()) {
//                                // 重新渲染keyTree
//                                mrRedisEditorJpanelLeft.resetPageIndex();
//                                mrRedisEditorJpanelLeft.renderKeyTree("*", null);
//                            }
//                        }
//                        break;
//
//                    default:
//                        valueString = newKeyDialog.getHashValueTextArea().getText();
//                        String field = newKeyDialog.getFieldTextField().getText();
//                        if (StringUtils.isEmpty(valueString)) {
//                            ErrorDialog.show("Value can not be empty");
//                        } else if (StringUtils.isEmpty(field)) {
//                            ErrorDialog.show("Field can not be empty");
//                        } else {
//                            redisPoolManager.hset(key, field, valueString, dbInfo.getIndex());
//                            // 关闭对话框
//                            newKeyDialog.close(OK_EXIT_CODE);
//                            if (newKeyDialog.isReloadSelected()) {
//                                // 重新渲染keyTree
//                                mrRedisEditorJpanelLeft.resetPageIndex();
//                                mrRedisEditorJpanelLeft.renderKeyTree("*", null);
//                            }
//                        }
//                }
//            } catch (Exception exp) {
//                ErrorDialog.show(exp.getMessage() + "");
//            }
//        });
//        newKeyDialog.show();
        new MrRedisNewKeyDialog(mrRedisEditorJpanelLeft.getProject(), mrRedisEditorJpanelLeft).ofDialogShow();
    }
}
